package com.entrogy.dao;

import com.alibaba.fastjson.JSONObject;
import com.entrogy.bean.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface UserMapper {

    @Select("select * from user where mobile = #{mobile} and delete_status=0")
    UserEntity getUserByMobile(@Param(value = "mobile") String mobile);

    int insertUser(EditUserEntity userEntity);

    //查询出某个用户的角色信息以及权限信息用于存入shiro
    UserEntity getUserRolesAndPermissions(@Param(value = "phone") String phone, @Param(value = "roleId") Long roleId);

    List<UserEntity> findUsers(@Param(value = "teacherName") String teacherName, @Param(value = "mobile") String mobile);

//    String findUserIdByMobile(String mobile);

    int addUserAndRole(@Param(value = "uid") String addUserId,@Param(value = "rid") long roleId);

    int updateUser(EditUserEntity userEntity);

    int deleteRolesWithUserId(String userId);

    int insertRoleWithUserId(@Param(value = "roleId")Integer roleId, @Param(value = "userId")String userId);

    int countRoleByUserId(String userId);

    int deleteUser(String userId);

    List<UserEntity> findUsersRowBound(@Param(value = "teacherName") String teacherName, @Param(value = "mobile") String mobile, @Param(value = "schoolCode") Integer schoolCode, @Param(value = "teacherId") String teacherId);

    int countUser(@Param("schoolCode") Integer schoolCode, @Param("teacherName") String teacherName);

    /**
     *  根据用户id查询用户信息
     * @param dingdingId
     * @return
     */
    @Select("select * from user where dingdingId = #{dingdingId}")
    UserEntity getUserById(@Param(value = "dingdingId") String dingdingId);

    @Select("select * from user where id = #{id}")
    UserEntity getUserByUid(@Param("id") String uid);
    /**
     * 根据用户id 分配课程权限
     * @param course_id
     * @param id
     * @return
     */
    @Update("update user set course_id=#{course_id}  where id=#{id}")
    int  UpdateDistributionByUser(@Param(value="course_id") String  course_id,@Param(value="id") String  id);

    /**
     *   根据用户id查询课程信息
     * @param id
     * @return
     */
    @Select("select course_id from user where id = #{id}")
    int SelectDistributionByUser(@Param(value="id") String  id);

    @Select("select * from student_information s where s.school_code = #{schoolCode} and s.course_id = (select class_id from user where school_code = #{schoolCode} and id = #{uid})")
    List<StudentEntity> getStudentsByBanzhu(@Param("uid") String uid,@Param("schoolCode") int schoolCode);

    @Select("select u.id as uid,l.update_time,u.`name` from `user` u " +
            "inner join user_position up on u.id=up.user_id and up.position_id=16 " +
            "left join leaveschool_set l on u.id=l.uid " +
            "where u.school_code=#{schoolCode}")
    List<LeaveSchoolSetEntity> getLeaveSchool(@Param("schoolCode") Integer schoolCode);

    @Select("select p.sid from `user` u " +
            "inner  join user_position up on u.id=up.user_id" +
            "inner JOIN position_information p ON up.position_id=p.sid and u.school_code=p.school_code" +
            "where u.id=#{id}")
    List<Integer> findSidByUid(String uid);

    @Select("select id from user where class_id = #{classId} and school_code = #{schoolCode}")
    String getUserByClassID(@Param("classId") int classId,@Param("schoolCode") int schoolCode);

    TeacherBasicInfoEntity getTeacherIdByParentId(@Param(value = "parentId") String parentId, @Param(value = "schoolCode") Integer schoolCode, @Param(value = "classId") Integer classId);

    TeacherBasicInfoEntity getTeacherIdByClassId(Map<String, Integer> params);

    JSONObject getClassInfo(String uid);

    String getTeacherPositionId(Integer schoolCode);

    /**
     * 根据用户id查询schoolCode
     * @param userId
     * @return
     */
    Integer getSchoolCodeByUserId(String userId);

    /**
     * 获取该校所有教职员工
     * @param schoolCode
     * @return
     */
    List<JSONObject> listSchoolAllUser(Integer schoolCode);

    /**
     * 修改用户密码
     * @param userId
     * @param newPassword
     * @return
     */
    int updateUserPassword(@Param(value = "userId") String userId, @Param(value = "newPassword") String newPassword);

    int cancelWeiXinAndPhoneBind(String mobile);

    /**
     * 查询教师信息
     * @param schoolCode
     * @param gradeId
     * @param classId
     * @param teacherIds
     * @param teacherName
     * @return
     */
    List<UserEntity> listSchoolTeacher(@Param(value = "schoolCode") Integer schoolCode, @Param(value = "gradeId") Integer gradeId, @Param(value = "classId") Integer classId, @Param(value = "teacherIds") String[] teacherIds, @Param("teacherName") String teacherName);

    /**
     * 更新教师的实义德id
     * @param userId
     * @param thdId
     * @return
     */
    boolean updateUserThdid(@Param("userId") String userId, @Param("thdId") int thdId);

    /**
     * 查询教师
     * @param schoolCode
     * @param grade_id
     * @param class_id
     * @param teacherName
     * @return
     */
    String getUserId(@Param("schoolCode") Integer schoolCode, @Param("gradeId") String grade_id, @Param("classId") Integer class_id, @Param("teacherName") String teacherName);

    /**
     * 查询某个学校具有某项权限的人员id
     * @param schoolCode
     * @param permissionCode
     * @return
     */
    List<UserEntity> listHasSomePermissionUserIdList(@Param("schoolCode") Integer schoolCode, @Param("permissionCode") String permissionCode);

    /**
     * 插入教师注册/修改申请(基本信息)
     * @param teacherInformationChange
     * @return
     */
    int insertTeacherInfoChange(TeacherInformationChangeEntity teacherInformationChange);

    /**
     * 插入教师注册/修改申请(高级)
     * @param id
     * @param teacherAdvancedInfoChangeList
     * @return
     */
    int insertTeacherAdvancedInfoChange(@Param("teacherInfoChangeId") Integer id, @Param("teacherAdvancedInfoChangeList") List<TeacherAdvancedInfoChangeEntity> teacherAdvancedInfoChangeList);

    /**
     * 查询教师信息变更/注册信息详情
     * @param changeId
     * @return
     */
    TeacherDetailInfoEntity getTeacherInformationChangeDetail(@Param("changeId") Integer changeId);

    /**
     * 查询教师列表
     * @param schoolCode
     * @param teacherId
     * @param positionId
     * @return
     */
    List<TeacherBasicInfoEntity> listTeacher(@Param("schoolCode") Integer schoolCode, @Param("teacherId") String teacherId, @Param("positionId") Integer positionId);

    /**
     * 查询教师是否有信息变更记录
     * @param teacherId
     * @param agreed
     * @return
     */
    Integer getUserInfoChangeStatus(@Param("teacherId") String teacherId, @Param("agreed") Integer agreed);

    /**
     * 查询教师信息变更（注册或信息变更）记录列表
     * @param schoolCode
     * @param changeType
     * @param teacherId
     * @return
     */
    List<TeacherBasicInfoEntity> listRegisterTeacher(@Param("schoolCode") Integer schoolCode, @Param("changeType") Integer changeType, @Param("teacherId") String teacherId);

    /**
     * 查询教师表的信息
     * @return
     * @param teacherId
     */
    TeacherDetailInfoEntity getTeacher(String teacherId);

    /**
     * 得到部门更改列表
     * @param changeId
     * @return
     */
    List<PositionEntity> listPositionChangeList(Integer changeId);

    /**
     * 得到课程更改列表
     * @param changeId
     * @return
     */
    List<CourseEntity> listCourseChangeList(Integer changeId);

    /**
     * 更新用户信息变更申请状态
     * @param changeId
     * @param agreed
     * @param opinion
     * @param roleIds
     * @return
     */
    int updateInfoChangeStatus(@Param("changeId") Integer changeId, @Param("agreed") Integer agreed, @Param("opinion") String opinion, @Param("roleIds") String roleIds);

    /**
     * 根据申请人id或者申请新增的电话号码得到处于待审核变更记录id
     * @param applyUserId
     * @param newPhone
     * @return
     */
    Integer getTeacherInfoChangeRecord(@Param("applyUserId") String applyUserId, @Param("newPhone") String newPhone);

    /**
     * 根据用户id获取名字
     * @param userId
     * @return
     */
    String getUserNameById(String userId);

    /**
     * 查询该校处于待审核的信息id
     * @param schoolCode
     * @return
     */
    List<Integer> listChangeId(Integer schoolCode);

    /**
     * 查询该校的职位列表
     * @param schoolCode
     * @return
     */
    List<TeacherPositionEntity> listSchoolPosition(Integer schoolCode);

    /**
     * 查询属于该职位的教师人数
     * @param schoolCode
     * @param positionId
     * @return
     */
    Integer countPositionTeacher(@Param("schoolCode") Integer schoolCode, @Param("positionId") Integer positionId);

    /**
     * 查询属于该职位的教师
     * @param schoolCode
     * @param positionId
     * @return
     */
    List<TeacherBasicInfoEntity> listSchoolTeacherForPosition(@Param("schoolCode") Integer schoolCode, @Param("positionId") Integer positionId);

    /**
     * 查询教师通讯录
     * @param schoolCode
     * @return
     */
    List<TeacherBasicInfoEntity> listTeacherContact(Integer schoolCode);

    /**
     * 更新用户信息
     * @param teacherId
     * @param teacherPhotoUrl
     * @return
     */
    boolean updateUserInfo(@Param("teacherId") String teacherId, @Param("teacherPhotoUrl") String teacherPhotoUrl);

    /**
     * 查询批量导入的excel的教师数据
     * @param schoolCode
     * @return
     */
    List<JSONObject> listExcelData(Integer schoolCode);

    /**
     * 查询变更记录
     * @param changeId
     * @return
     */
    TeacherInformationChangeEntity listTeacherChangeRecord(@Param("changeId") Integer changeId);

    /**
     * 查询既是家长又是老师的情况
     * @param phone
     * @return
     */
    List<String> listUserIdentity(@Param("phone") String phone);

    /**
     * 查询教师信息（运维查询）
     * @param schoolCode
     * @param teacherPhone
     * @param teacherName
     * @return
     */
    List<TeacherBasicInfoEntity> listTeacherForMaintenance(@Param("schoolCode") Integer schoolCode, @Param("teacherPhone") String teacherPhone, @Param("teacherName") String teacherName);

    /**
     * 修改教师信息
     * @param teacher
     * @return
     */
    int updateUserDetail(TeacherBasicInfoEntity teacher);
}
